<script lang="ts" setup>
import type { NotificationItem } from './notification';

import { computed, onMounted, ref } from 'vue';
import { useRouter } from 'vue-router';
// import { resetAllStores, useAccessStore, useUserStore } from '@vben/stores';

import { AuthenticationLoginExpiredModal } from '@vben/common-ui';
import { PhUserCircle } from '@vben/icons';
import { BasicLayout, LockScreen, UserDropdown } from '@vben/layouts';
import { preferences } from '@vben/preferences';
import { storeToRefs, useAccessStore, useUserStore } from '@vben/stores';

import { useAuthStore } from '#/store';

const router = useRouter();

const notifications = ref<NotificationItem[]>([
  // {
  //   avatar: "https://avatar.vercel.sh/vercel.svg?text=VB",
  //   date: "3小时前",
  //   isRead: true,
  //   message: "描述信息描述信息描述信息",
  //   title: "收到了 14 份新周报",
  // },
  // {
  //   avatar: "https://avatar.vercel.sh/1",
  //   date: "刚刚",
  //   isRead: false,
  //   message: "描述信息描述信息描述信息",
  //   title: "朱偏右 回复了你",
  // },
  // {
  //   avatar: "https://avatar.vercel.sh/1",
  //   date: "2024-01-01",
  //   isRead: false,
  //   message: "描述信息描述信息描述信息",
  //   title: "曲丽丽 评论了你",
  // },
  // {
  //   avatar: "https://avatar.vercel.sh/satori",
  //   date: "1天前",
  //   isRead: false,
  //   message: "描述信息描述信息描述信息",
  //   title: "代办提醒",
  // },
]);

const userStore = useUserStore();
const authStore = useAuthStore();
const accessStore = useAccessStore();
const showDot = computed(() =>
  notifications.value.some((item) => !item.isRead),
);

const menus = computed(() => [
  {
    handler: () => {
      router.push('/user/center');
    },
    icon: PhUserCircle,
    text: '个人中心',
  },
  // {
  //   handler: () => {
  //     openWindow(VBEN_GITHUB_URL, {
  //       target: '_blank',
  //     });
  //   },
  //   icon: MdiGithub,
  //   text: 'GitHub',
  // },
  // {
  //   handler: () => {
  //     openWindow(`${VBEN_GITHUB_URL}/issues`, {
  //       target: '_blank',
  //     });
  //   },
  //   icon: CircleHelp,
  //   text: $t('widgets.qa'),
  // },
]);

const { loginLoading } = storeToRefs(authStore);

const avatar = computed(() => {
  return userStore.userInfo?.avatar ?? preferences.app.defaultAvatar;
});

async function handleLogout() {
  await authStore.logout(false);
}

const isDev = import.meta.env.MODE === 'development';
const value = ref(localStorage.getItem('@@@proxy_type') || ''); // /zp

if (isDev && value.value === '') {
  localStorage.setItem('@@@proxy_type', '/test');
}

onMounted(async () => {
  // const data = await Apis.home.get_todo({
  //   params: { pageNum: 1, pageSize: 20 },
  // });
  // notifications.value = data.rows.map((item) => {
  //   let module =
  //     getDictObj(DICT_TYPE.contract_todo_type, item.module)?.label || '';
  //   module = module.slice(-4);
  //   let moduleTextArr = [...module];
  //   moduleTextArr =
  //     moduleTextArr.length <= 3 ? moduleTextArr : moduleTextArr.slice(-4);
  //   return {
  //     moduleTextArr,
  //     avatar: '',
  //     date: item.createTime,
  //     isRead: true,
  //     message: item.contractName,
  //     title: item.taskName,
  //   };
  // });
  // console.log(notifications);
  // resetAllStores();
});
</script>

<template>
  <BasicLayout @clear-preferences-and-logout="handleLogout">
    <template #user-dropdown>
      <UserDropdown
        :avatar
        :description="userStore.userInfo?.belongOrgName"
        :menus
        :tag-text="userStore.userInfo?.gwmc"
        :text="userStore.userInfo?.displayName"
        @logout="handleLogout"
      />
    </template>
    <template #notification>
      <!-- <a-dropdown v-if="isDev">
        <template #overlay>
          <a-menu selectable @click="handleMenuClick">
            <a-menu-item key="test"> 测试 </a-menu-item>
            <a-menu-item key="pro"> 正式 </a-menu-item>
            <a-menu-item key="czg"> czg </a-menu-item>
            <a-menu-item key="zp"> zp </a-menu-item>
            <a-menu-item key="zzz"> zzz </a-menu-item>
            <a-menu-item key="xmh"> xmh </a-menu-item>
          </a-menu>
        </template>
        <a-button> {{ value ? `代理${value}` : '代理切换' }} </a-button>
      </a-dropdown> -->

      <!-- <Notification
        :dot="showDot"
        :notifications="notifications"
        @clear="handleNoticeClear"
        @make-all="handleMakeAll"
        @view-all="handleViewAll"
      /> -->
    </template>
    <template #extra>
      <AuthenticationLoginExpiredModal
        v-model:open="accessStore.loginExpired"
        :avatar
        :loading="loginLoading"
        password-placeholder=""
        username-placeholder=""
        @submit="authStore.authLogin"
      />
    </template>
    <template #lock-screen>
      <LockScreen
        :avatar
        :text="userStore.userInfo?.displayName"
        @to-login="handleLogout"
      />
    </template>
  </BasicLayout>
</template>
